package com.msg.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.msg.actions.ActionHelper;
import com.msg.beans.ForecastBean;
import com.msg.beans.PriceBean;
import com.msg.beans.SeasonForecastBean;
import com.msg.beans.TicketGroupBean;

/**
 * Servlet implementation class ActionController
 */
@WebServlet("/CSVController")
public class CSVController extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	public CSVController() {
		super();
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
	{
		String valueStr = request.getParameter("exp");
		
		if (request.getParameter("action").equals("CSV")) {
			DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
			Date date = new Date();
			String dateF = dateFormat.format(date);
			response.setHeader("Content-Type", "text/csv");
			response.setHeader("Content-Disposition", "attachment; filename = "
					+ request.getParameter("type") + dateF + ".csv");
			PrintWriter writer = response.getWriter();
			writer.write(valueStr);
			writer.flush();
			writer.close();
		}
		else if(request.getParameter("action").equals("SAVE")){
		   this.saveEdit(valueStr,request.getParameter("type")); 
		}
	}
	
	private void saveEdit(String valueStr, String type) {
		
		ActionHelper ah = new ActionHelper();
		
		if (type.equals("SeasonForecast")) {
			ArrayList<SeasonForecastBean> sfList = new ArrayList<SeasonForecastBean>();
			String rowsplit[], sfsplit[];
			String docStr = valueStr;
			rowsplit = docStr.split("[\r\n]+");

			int i = 0;
			for (String s : rowsplit) {
				SeasonForecastBean sf = new SeasonForecastBean();
				if (i != 0) {
					sfsplit = s.split("[,]");
					for (int j = 0; j < sfsplit.length; j++) {
						if (j == 0)
							sf.setSeason(sfsplit[j]);
						if (j == 2)
							sf.setEvent_Date(sfsplit[j]);
						if (j == 20)
							sf.setBudget(Integer.parseInt(sfsplit[j]));
						if (j == 21)
							sf.setBudget_Tickets(Integer.parseInt(sfsplit[j]));
					}
					System.out.println(sf.toString());
					sfList.add(sf);
				}
				
				i++;
			}
			
			ah.saveSeasonForecast(sfList);
		}
		
		if (type.equals("Price")) {
			
			System.out.println("CSV Controller : Price");
			ArrayList<PriceBean> pList = new ArrayList<PriceBean>();
			String rowsplit[], psplit[];
			String docStr = valueStr;
			rowsplit = docStr.split("[\r\n]+");

			int i = 0;
			for (String s : rowsplit) {
				
				System.out.println("Each row"+s);
				PriceBean pf = new PriceBean();
				if (i != 0) {
					psplit = s.split("[,]");
					for (int j = 0; j < psplit.length; j++) {
						if (j == 0)
							pf.setTicket_Group(psplit[j]);
						if (j == 1)
							pf.setTicket_Level(psplit[j]);
						if (j == 2)
							pf.setPrice(Integer.parseInt(psplit[j]));
						
					}
					System.out.println(pf.toString());
					pList.add(pf);
				}
				
				i++;
			}
			
			ah.savePrice(pList);
		}

		
		if (type.equals("TicketGroup")) {
			
			System.out.println("CSV Controller : TicketGroup");
			ArrayList<TicketGroupBean> tgList = new ArrayList<TicketGroupBean>();
			String rowsplit[], psplit[];
			String docStr = valueStr;
			rowsplit = docStr.split("[\r\n]+");

			int i = 0;
			for (String s : rowsplit) {
				
				System.out.println("Each row"+s);
				TicketGroupBean pf = new TicketGroupBean();
				if (i != 0) {
					psplit = s.split("[,]");
					for (int j = 0; j < psplit.length; j++) {
						if (j == 0)
							pf.setTicketTypeCode(psplit[j]);
						if (j == 1)
							pf.setPriceLevelCode(psplit[j]);
						if (j == 2)
							pf.setTicketGroupName((psplit[j]));
						
					}
					System.out.println(pf.toString());
					tgList.add(pf);
				}
				
				i++;
			}
			
			ah.saveTicketGroup(tgList);
		}
		
		
		if (type.equals("Forecast")) {
			
		//	System.out.println(valueStr);
			ArrayList<ForecastBean> fList = new ArrayList<ForecastBean>();
			String rowsplit[], fsplit[];
			String docStr = valueStr;
			String event = "";
			rowsplit = docStr.split("[\r\n]+");

			int i = 0;
			for (String s : rowsplit) {
				ForecastBean f = new ForecastBean();
				if (i == 0) 
				{
				fsplit = s.split("[,]");
				event = fsplit[0];
				System.out.println("Event :"+event);
				}
				else if( i == 5 ||i == 6 ||i == 7 ||i == 10||i == 11  ||i == 9 )
				{	fsplit = s.split("[,]");
					for (int j = 0; j < fsplit.length; j++) {
						if (j == 0)
							f.setTicket(fsplit[j]);
						if (j == 1)
							f.setAmount(Integer.parseInt(fsplit[j]));
						if (j == 2)
							f.setTotal_Seats(Integer.parseInt(fsplit[j]));
						if (j == 3)
							f.setA(Integer.parseInt(fsplit[j]));
						if (j == 4)
							f.setB(Integer.parseInt(fsplit[j]));
						if (j == 5)
							f.setC(Integer.parseInt(fsplit[j]));
						if (j == 6)
							f.setD(Integer.parseInt(fsplit[j]));
						if (j == 7)
							f.setE(Integer.parseInt(fsplit[j]));
						if (j == 8)
							f.setF(Integer.parseInt(fsplit[j]));
						if (j == 9)
							f.setG(Integer.parseInt(fsplit[j]));
						if (j == 10)
							f.setH(Integer.parseInt(fsplit[j]));
					}
					System.out.println(f.toString());
					fList.add(f);
				}
				
				i++;
			}
			
			ah.saveForecast(fList, event);
			
		}
		
	}
}